package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Item;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;
import com.feidian.helper.ui.NewsDetailsAcceptActivity.GiveUpObjectThread;
import com.feidian.helper.ui.fragment.MessageFragment;
import com.feidian.helper.ui.fragment.NewSettingFragment;
import com.feidian.helper.ui.fragment.SendFragment;
import com.feidian.helper.util.Constant;

public class NewsDetailsSendActivity extends Activity implements
		View.OnClickListener {
	
	private ImageView mBackView;
	private TextView mTitle;

	private TextView mNameTv;
	private TextView mStartTimeTv;
	private TextView mTitleTv;
	private TextView mContentTv;
	private TextView mStateTv;
	private TextView mQQTv;
	private TextView mPhoneTv;
	private TextView mPrice;
	private TextView mEndLine;
	private RatingBar ratingBar;
	private TextView mAcceptNameTv;
	private TextView mAcceptIdTv;
	private TextView mAcceptQQTv;
	private TextView mAcceptPhoneTv;
	private Button acceptBn;
	
	private String mQQ;
	private String mPhone;
	private String mHonesty;
	
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.FINISH_SUCCESS: {
				Toast.makeText(NewsDetailsSendActivity.this, "完成处理", Toast.LENGTH_LONG)
				.show();
				getItem().setState("2");
				finish();

			}
				break;
			
			case Constant.GET_PER_INF_SUCCESS:{
				try{
					JSONObject jsonObject=new JSONObject((String)msg.obj);
					mQQ=jsonObject.getString(Table.qq);
					mPhone=jsonObject.getString(Table.phone);
					if(mQQ!=null)
					{
						mAcceptQQTv.setText(mQQ);
					}
					if(mPhone!=null)
					{
						mAcceptPhoneTv.setText(mPhone);
					}
				}catch (Exception e)
				{
					e.printStackTrace();
				}
				
			}break;
			case Constant.FINISH_FAILURE: {
				Toast.makeText(NewsDetailsSendActivity.this, "未完成处理", Toast.LENGTH_LONG)
				.show();
			}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_details_send);
		initView();
	}

	private void initView() {
		mBackView = (ImageView) findViewById(R.id.back);
		mBackView.setVisibility(View.VISIBLE);
		mBackView.setOnClickListener(this);
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText("懒人帖子详情");
		mNameTv = (TextView) findViewById(R.id.name);

		mStartTimeTv = (TextView) findViewById(R.id.past_time);

		mTitleTv = (TextView) findViewById(R.id.my_title);

		mContentTv = (TextView) findViewById(R.id.content);

		mStateTv = (TextView) findViewById(R.id.state);

		mQQTv = (TextView) findViewById(R.id.qq);

		mPhoneTv = (TextView) findViewById(R.id.phone);

		mPrice = (TextView) findViewById(R.id.price);

		mEndLine = (TextView) findViewById(R.id.endline);

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);

		mAcceptNameTv = (TextView) findViewById(R.id.accept_name);
		mAcceptIdTv= (TextView) findViewById(R.id.accept_id);
		mAcceptQQTv= (TextView) findViewById(R.id.accept_qq);
		mAcceptPhoneTv= (TextView) findViewById(R.id.accept_phone);

		acceptBn = (Button) findViewById(R.id.accept_button);
		acceptBn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showFinishObjectDialog();
			}
		});

		upData();

	}

	void upData() {
		if (getItem().getName() != null
				|| !getItem().getName().trim().equals("null"))
			mNameTv.setText(getItem().getName());
		else
			mNameTv.setText("出错");
		mStartTimeTv.setText(getItem().getPastTime());
		mTitleTv.setText(getItem().getTitle());
		mContentTv.setText(getItem().getContent());
		mQQTv.setText(getItem().getQQ().equals("null")?"无":getItem().getQQ());
		mPhoneTv.setText(getItem().getTel().equals("null")?"无":getItem().getTel());
		mPrice.setText(getItem().getPrice().equals("null")?"无":getItem().getPrice());
		mEndLine.setText(getItem().getEndline());
		if (getItem().getAccepName() != null
				|| getItem().getAccepName().equals("null"))
			mAcceptNameTv.setText(getItem().getAccepName().equals("null")?"无":getItem().getAccepName());
		if(getItem().getAccepStuid()!=null&&!getItem().getAccepStuid().equals("null"))
		{
			mAcceptIdTv.setText(getItem().getAccepStuid());
		}
		if(mQQ!=null)
		{
			mAcceptQQTv.setText(mQQ);
		}
		if(mPhone!=null)
		{
			mAcceptPhoneTv.setText(mPhone);
		}
		Log.e("pos="+SendFragment.pos,SendFragment.pos+"");
		if (getItem().getState().equals("0")) {
			mStateTv.setText("未接单");
			acceptBn.setText("是否完成处理");
		} else if (getItem().getState().equals("1")) {
			mStateTv.setText("处理中");
			acceptBn.setText("是否完成处理");
		} else if (getItem().getState().equals("2")) {
			mStateTv.setText("已经处理");
			acceptBn.setText("处理完成");
			acceptBn.setFocusable(false);
			acceptBn.setBackgroundColor(Color.GRAY);
		} else {
			mStateTv.setText("出错");
			acceptBn.setText("出错");
			acceptBn.setFocusable(false);
			acceptBn.setBackgroundColor(Color.GRAY);
		}

		if (getItem().getHonesty() != null
				&& !getItem().getHonesty().equals("null")) {
			ratingBar.setRating(Float.valueOf(getItem().getHonesty()));
		}

		else {
			ratingBar.setRating(5);
		}
		if (getItem().getState().equals("1")) {
			String url = "http://115.29.103.105/User/getPerInf";
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(Table.stuId,getItem().getAccepStuid()));
			UnionThread unionThread = new UnionThread(url, params,Constant.GET_PER_INF);
			unionThread.start();
			
			/*
			GetPerInfThread getPerInfThread=new GetPerInfThread(url, params);
			getPerInfThread.start();
			*/
		}
		Log.e("NewsDetailsSendActivity","getPerInfThread");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}

	Item getItem() {
		return SendFragment.itemList.get(SendFragment.pos);
	}
	
	/*
	 * 优化
	 */
	
	class UnionThread extends Thread
	{
		private String mUrl;
		private List<NameValuePair> mParams;
		private int kind;
		public  UnionThread(String url,List<NameValuePair> params,int kind) {
			// TODO Auto-generated constructor stub
			mUrl=url;
			mParams=params;
			this.kind = kind;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			switch(kind) {
			case Constant.GET_PER_INF:
				if (context != null) {
					Message msg = mHandler.obtainMessage();
					Log.e("context="+context,"....................");
					msg.obj = context;
					msg.what = Constant.GET_PER_INF_SUCCESS;
					mHandler.sendMessage(msg);
				}
				else
				{
					Log.e("context=", "null");
					Message msg = mHandler.obtainMessage();
					msg.what = Constant.GET_PER_INT_FAILURE;
					mHandler.sendMessage(msg);
				}
				break;
			case Constant.FINISH_OBJ:
				if (context != null&&context.equals("1")) {

					Log.e("contenxt("+NewsDetailsSendActivity.this.getClass().toString()+")=", context);
					Message msg = mHandler.obtainMessage();
					msg.obj = context;
					msg.what = Constant.FINISH_SUCCESS;
					mHandler.sendMessage(msg);
				}
				else
				{
					Log.e("context=", "null");
					Message msg = mHandler.obtainMessage();
					msg.what = Constant.FINISH_FAILURE;
					mHandler.sendMessage(msg);
				}
				break;
			}
			super.run();
		}
	}
	
	/*
	class GetPerInfThread extends Thread
	{
		private String mUrl;
		private List<NameValuePair> mParams;
		public  GetPerInfThread(String url,List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl=url;
			mParams=params;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null) {
				Message msg = mHandler.obtainMessage();
				Log.e("context="+context,"....................");
				msg.obj = context;
				msg.what = GET_PER_INF_SUCCESS;
				mHandler.sendMessage(msg);
			}
			else
			{
				Log.e("context=", "null");
				Message msg = mHandler.obtainMessage();
				msg.what = GET_PER_INT_FAILURE;
				mHandler.sendMessage(msg);
			}
				
			super.run();
		}
	}
	*/
	/*
	class FinishObjectThread extends Thread
	{
		private String mUrl;
		private List<NameValuePair> mParams;
		public  FinishObjectThread(String url,List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl=url;
			mParams=params;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null&&context.equals("1")) {

				Log.e("contenxt("+NewsDetailsSendActivity.this.getClass().toString()+")=", context);
				Message msg = mHandler.obtainMessage();
				msg.obj = context;
				msg.what = FINISH_SUCCESS;
				mHandler.sendMessage(msg);
			}
			else
			{
				Log.e("context=", "null");
				Message msg = mHandler.obtainMessage();
				msg.what = FINISH_FAILURE;
				mHandler.sendMessage(msg);
			}
				
			super.run();
		}
	}
	*/
	private void showFinishObjectDialog() {

		final Dialog mDialog = new Dialog(NewsDetailsSendActivity.this,
				R.style.dialog);
		LayoutInflater layoutInflater = NewsDetailsSendActivity.this
				.getLayoutInflater();
		View view = layoutInflater.inflate(R.layout.logout_dialog, null, false);
		TextView content=(TextView)view.findViewById(R.id.content);
		content.setText("单子是否处理完成?");
		Button mSure = (Button) view.findViewById(R.id.sure);
		Button mCancel = (Button) view.findViewById(R.id.no);
		mSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (getItem().getState().equals("1")) {
					String url = "http://115.29.103.105/User/finish";
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					Log.e("stuId=",User.stuId+"");
					Log.e("id=",getItem().getId());
					params.add(new BasicNameValuePair(Table.stuId, User.stuId));
					params.add(new BasicNameValuePair(Table.id, getItem()
							.getId()));
					UnionThread unionThread = new UnionThread(url,params,Constant.FINISH_OBJ);
					unionThread.start();
					/*
					FinishObjectThread finishObjectThread = new FinishObjectThread(
							url, params);
					finishObjectThread.start();
					*/
				}
			}
		});
		mCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mDialog.dismiss();
			}
		});
		mDialog.setContentView(view);
		mDialog.show();
	}
}
